package com.cyan.note.controller.manager;

import com.cyan.note.common.ApiResult;
import com.cyan.note.common.PageResult;
import com.cyan.note.dto.manager.service.ServiceQuickDTO;
import com.cyan.note.entity.Service;
import com.cyan.note.service.manager.ManagerServiceService;
import com.cyan.note.util.SecurityUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店长端-服务管理控制器
 * 
 * @author ZhangzhenYu
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/api/manager/services")
public class ManagerServiceController {

    @Autowired
    private ManagerServiceService managerServiceService;

    /**
     * 分页查询服务列表
     */
    @GetMapping("/list")
    public ApiResult<PageResult<Service>> getServiceList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String serviceType,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(defaultValue = "display_order") String sortBy,
            @RequestParam(defaultValue = "asc") String sortOrder
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerServiceService.getServiceList(shopId, page, size, keyword, status, 
                serviceType, categoryId, sortBy, sortOrder);
    }

    /**
     * 查询服务详情
     */
    @GetMapping("/{id}")
    public ApiResult<Service> getServiceDetail(@PathVariable Long id) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerServiceService.getServiceDetail(shopId, id);
    }

    /**
     * 新增服务
     */
    @PostMapping
    public ApiResult<Void> createService(@Valid @RequestBody Service service) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        service.setShopId(shopId);
        service.setCreatedBy(operatorId);
        return managerServiceService.createService(service, operatorId);
    }

    /**
     * 更新服务信息
     */
    @PutMapping("/{id}")
    public ApiResult<Void> updateService(@PathVariable Long id, @Valid @RequestBody Service service) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        service.setId(id);
        service.setShopId(shopId);
        return managerServiceService.updateService(service, operatorId);
    }

    /**
     * 删除服务
     */
    @DeleteMapping("/{id}")
    public ApiResult<Void> deleteService(@PathVariable Long id) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerServiceService.deleteService(shopId, id, operatorId);
    }

    /**
     * 查询快速选择服务列表（用于手动下单）
     */
    @GetMapping("/quick")
    public ApiResult<List<ServiceQuickDTO>> getQuickServices() {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerServiceService.getQuickServices(shopId);
    }

    /**
     * 更新服务状态
     */
    @PutMapping("/{id}/status")
    public ApiResult<Void> updateServiceStatus(
            @PathVariable Long id,
            @RequestParam String status
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerServiceService.updateServiceStatus(shopId, id, status, operatorId);
    }

    /**
     * 复制服务
     */
    @PostMapping("/{id}/copy")
    public ApiResult<Void> copyService(@PathVariable Long id) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerServiceService.copyService(shopId, id, operatorId);
    }

    /**
     * 批量更新服务排序
     */
    @PutMapping("/batch-sort")
    public ApiResult<Void> batchUpdateSort(@RequestBody List<Long> serviceIds) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerServiceService.batchUpdateSort(shopId, serviceIds, operatorId);
    }
}